﻿#include "pch.h"
#if _QG_ENABLE_WIDGET
#include "qgp/qgach.h"
#include "qgp/qgaud.h"
#include "qgp/qguwgs.h"

//////////////////////////////////////////////////////////////////////////
// 라벨
KIMPL_SUBCLASS(qwProgress, "WidgetProgress");

qwProgress::qwProgress(qgWidgetType type)
	: super_type(type)
	, _pos(0)
	, _range(0, 10)
	, _margin(2, 2)
{
	_insts.fill = true;
	_insts.border = true;

	_blend.advance = 1.0f;
	_blend.speed = 20.0f;
}

void qwProgress::OnInitialize()
{
	super_type::OnInitialize();

	_rvalue = _shed->GetResource(QGWELM_PROGRESS_VALUE);
}

bool qwProgress::OnUpdate(float adv)
{
	bool valueupdate = false;

	BuildBound();

	if (NeedRefresh() || _drag)
	{
		// 배경
		_bback = !CanFill() ? _client : _client.Deflate(_margin.x, _margin.y);

		// 찍을 모양
		float len = (float)GetLength();
		float rate = (float)(_pos - _range.min) / len;

		_blend.width = (float)_bback.Width() * rate;

		// 글자
		if (HasExtInfo())
			_text_str.Format("%d", _pos);

		if (HasText())
			BuildTextAnch();

		//
		valueupdate = true;

		ResetRefresh();
	}

	if (valueupdate || _blend.advance > 0.0f)
	{
		_blend.advance -= adv*_blend.speed;

		if (_blend.advance < 0.0f)
			_blend.advance = 0.0f;

		_blend.lerp = k_lerp(_blend.last, _blend.width, 1.0f - _blend.advance);
		_bvalue.Set(_bback.left, _bback.top, _bback.left + (kint)_blend.lerp, _bback.bottom);
	}

	return true;
}

void qwProgress::OnDraw()
{
	// 배경
	if (CanFill())
		_shed->DrawFillRect(_client, qgWidgetObj::GetResource(), _tex);

	// 진행 상태
	_shed->DrawFillRect(_bvalue, _rvalue, _tex);

	// 외곽선
	if (HasBorder())
		_shed->DrawEmptyRect(_client, _color_border);

	// 글자
	if (HasText())
		_shed->DrawAnchText(_font, _text_str.Data(), _bback, _text_info, 0);

	qwCtrl::OnDraw();
}

//
const qbWidgetResource& qwProgress::GetResource(qgWidgetElement nth)
{
	if (nth == QGWELM_PROGRESS_VALUE)
		return _rvalue;

	return super_type::GetResource(nth);
}

//
void qwProgress::SetPosition(kint pos, bool initvalue)
{
	_pos = K_CLAMP(pos, _range.min, _range.max);

	if (initvalue)
	{
		// 음... 어케 고치지, 일단 아래 내용은 임시
		_blend.last = _blend.lerp;
		_blend.advance = 1.0f;
	}
	else
	{
		_blend.last = _blend.lerp;
		_blend.advance = 1.0f;
	}

	SetRefresh();
}

//
void qwProgress::SetPosition(qgWidgetPlace place)
{
	switch (place)
	{
		case QGWPLACE_LEFT:
			_pos = _range.min;
			break;

		case QGWPLACE_RIGHT:
			_pos = _range.max;
			break;

		case QGWPLACE_CENTER:
			_pos = _range.min + GetLength() / 2;
			break;
	}

	SetRefresh();
}

//
void qwProgress::SetRange(const tpl::Point& p)
{
	kint rmin = K_MIN(p.min, p.max);
	kint rmax = K_MAX(p.min, p.max);

	_range.Set(p.min, p.max);
	_pos = K_CLAMP(_pos, rmin, rmax);

	_blend.last = _blend.lerp;
	_blend.advance = 1.0f;

	SetRefresh();
}

//
bool qwProgress::SetResource(qgWidgetElement nth, const qbWidgetResource& res)
{
	if (nth == QGWELM_PROGRESS_VALUE)
	{
		_rvalue = res;
		return true;
	}

	return super_type::SetResource(nth, res);
}

//
kint qwProgress::ValidatePosition(kint xloc)
{
	kint t = xloc - _bback.left;
	float w = (float)_bback.Width();
	float l = (float)(_range.max - _range.min);
	float pos = (float)t / w * l + 0.5f;
	kint rpos = (kint)pos + _range.min;
	return K_CLAMP(rpos, _range.min, _range.max);
}

//
bool qwProgress::OnMouseMove(kint x, kint y, const tpl::Point& relative, kushort state)
{
	if (_drag)
	{
		_pos = ValidatePosition(x);

		_blend.last = _blend.lerp;
		_blend.advance = 1.0f;

		if (_ecb_position_changed)
			_ecb_position_changed(this, _pos);

		return false;
	}

	return true;
}

//
bool qwProgress::OnMouseDown(kint x, kint y, kushort button)
{
	if (button == QIM_LEFT)
	{
		if (_bback.OnPoint(x, y))
		{
			_pos = ValidatePosition(x);

			_blend.last = _blend.lerp;
			_blend.advance = 1.0f;

			if (_ecb_position_changed)
				_ecb_position_changed(this, _pos);

			_drag = true;

			return false;
		}
	}

	return true;
}

//
bool qwProgress::OnMouseUp(kint x, kint y, kushort button)
{
	if (button == QIM_LEFT)
	{
		_drag = false;

		SetRefresh();
	}

	return true;
}

#endif	// _QG_ENABLE_WIDGET
